package com.jinkeen.xiaxie.customView

import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import com.jinkeen.xiaxie.R

/**
 * 呼吸灯效果
 */
class BreathLight @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), AnimatorUpdateListener {
    private val mCenterCircleRadius: Float
    private var mMaxCircleRadius: Float
    private val mCirclePaint: Paint
    private var mAlphaValue = 0

    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.BreathView)
        mCenterCircleRadius = a.getDimension(R.styleable.BreathView_centerCircleRadius, 5f)
        mMaxCircleRadius = a.getDimension(R.styleable.BreathView_maxCircleRadius, 10f)
        if (mCenterCircleRadius >= mMaxCircleRadius) {
            mMaxCircleRadius = mCenterCircleRadius * 2
        }
        val circleColor = a.getColor(R.styleable.BreathView_circleColor, Color.GREEN)
        a.recycle()
        mCirclePaint = Paint()
        mCirclePaint.isAntiAlias = true
        mCirclePaint.style = Paint.Style.FILL
        mCirclePaint.color = circleColor
        val circleAlphaValueAnimator = ValueAnimator.ofInt(0, 255)
        circleAlphaValueAnimator.duration = BREATH_TIME
        circleAlphaValueAnimator.repeatCount = Animation.INFINITE
        circleAlphaValueAnimator.repeatMode = ValueAnimator.REVERSE
        circleAlphaValueAnimator.interpolator = LinearInterpolator()
        circleAlphaValueAnimator.addUpdateListener(this)
        circleAlphaValueAnimator.start()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            width = Math.max(width.toFloat(), mMaxCircleRadius * 2).toInt()
        }
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            height = Math.max(width.toFloat(), mMaxCircleRadius * 2).toInt()
        }
        setMeasuredDimension(width, height)
    }

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        val centerX = width / 2.0f
        val centerY = height / 2.0f
        mCirclePaint.alpha = 255
        canvas.drawCircle(centerX, centerY, mCenterCircleRadius, mCirclePaint)
        mCirclePaint.alpha = mAlphaValue
        canvas.drawCircle(centerX, centerY, mMaxCircleRadius, mCirclePaint)
    }

    override fun onAnimationUpdate(valueAnimator: ValueAnimator) {
        mAlphaValue = valueAnimator.animatedValue as Int
        invalidate()
    }

    companion object {
        private const val BREATH_TIME: Long = 1000 //动画执行时间/呼吸速率
    }
}

